<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->setting;
if($_RQ['op']=='main'){
	$return['extradata']=array();
	//读取卡券列表
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$coupons=pdo_getall('jy_weishop_coupon',$params,$fields);
	if($coupons){
		foreach($coupons as &$row){
				if ($row['t'] != -1){
					if ($row['last'] <= 0) 
					{
						$row['last'] = 0;
						$row['isdisa'] = '1';
					}
					else 
					{
						$totle = $row['t'];
						$row['lastratio'] = intval(($row['last'] / $totle) * 100);
					}
				}else{
					$row['last'] = 1;
					$row['lastratio'] = 100;
				}
				if ($row['coupontype'] == '2') {
							if (0 < $row['enough']) 
							{
								$title2 = '充值满￥' . (double) $row['enough'] . '元可用';
							}
							else 
							{
								$title2 = '无金额门槛';
							}
				}
				if ($row['coupontype'] == '1') {
							if (0 < $row['enough']) 
							{
								$title2 = '满' . (double) $row['enough'] . '元可用';
							}
							else 
							{
								$title2 = '无金额门槛';
							}
				}
				if ($row['backtype'] == 0) {
							$title3 = '￥' . (double) $row['deduct'];
							if ($row['enough'] == '0') 
							{
								$title5 = '消费任意金额立减' . (double) $row['deduct'].'元';
								$row['color'] = 'red ';
								$tagtitle = '代金券';
							}
							else 
							{
								$title5 = '消费满￥' . (double) $row['enough'] . '立减' . (double) $row['deduct'].'元';
								$row['color'] = 'blue';
								$tagtitle = '满减券';
							}
				}elseif ($row['backtype'] == 1) {
							$row['color'] = 'red ';
							$title3 = (double) $row['discount'] . ' 折 ';
							$tagtitle = '打折券';
							if ($row['enough'] == '0') 
							{
								$title5 = '消费任意金额' . '打' . (double) $row['discount'] . '折';
							}
							else 
							{
								$title5 = '消费满￥' . (double) $row['enough'] . '打' . (double) $row['discount'] . '折';
							}
				}elseif ($row['backtype'] == 2) {
							if ($row['coupontype'] == '0') 
							{
								$row['color'] = 'red ';
								$tagtitle = '购物返现券';
							}
							elseif ($row['coupontype'] == '2') 
							{
								$row['color'] = 'pink ';
								$tagtitle = '充值返现券';
							}elseif ($row['coupontype'] == '1') 
							{
								$row['color'] = 'red ';
								$tagtitle = '购物返现券';
							}
							if ($row['enough'] == '0') 
							{	
								$title5 = $row['coupontype'] == 1 ? '消费任意金额' : '充值任意金额';
							}else {
								$title5 = $row['coupontype'] == 1 ? '消费满￥' . (double) $row['enough'] : '充值满￥' . (double) $row['enough'];
							}
							if (!(empty($row['backmoney'])) && (0 < $row['backmoney'])) 
							{
								$title3 = '立返';
								$title5 = $title5 . '立返余额￥'.$row['backmoney'];
							}
							elseif (!(empty($row['backcredit'])) && (0 < $row['backcredit'])) 
							{
								$title3 = '立返';
								$title5 = $title5 . '立返积分'.$row['backcredit'];
							}
							elseif (!(empty($row['backredpack'])) && (0 < $row['backredpack'])) 
							{
								$title5 = $title5 . '立返红包￥'.$row['backcredit'];
							}
				}
				if ($row['tagtitle'] == '') {
							$row['tagtitle'] = $tagtitle;
				}
				if ($row['timelimittype'] == '0') {
							$title4 = '永久有效';
				}elseif ($row['timelimittype'] == '1') {
							$title4 = '即' . $row['gettypestr'] . '日内' . $row['timedays'] . '天有效';
				}else {
							$row['limittimes']=json_decode($row['limittimes'],true);
							$title4 = $row['limittimes']['tstart'].'~'.$row['limittimes']['tend'];
				}
				$row['title2'] = $title2;
				$row['title3'] = $title3;
				$row['title4'] = $title4;
				$row['title5'] = $title5;
				
				//发放时间：
				$row['sendtime']=json_decode($row['sendtime'],true);
				if($row['issendtime']){
					if($row['sendtime']['tstart'] > time()){
						$row['issend'] = 0;
					}elseif($row['sendtime']['tstart'] < time() && $row['sendtime']['tend'] > time()){
						$row['issend'] = 1;
					}else{
						$row['issend'] = 2;
					}
				}else{
					$row['issend'] = 1;
				}
		}
	}
	$return['coupons']=$coupons;
	$lclassify=pdo_getall('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid']),'','','paixu asc');
	foreach($lclassify as &$item){
		$sql1 = "SELECT SUM(area) FROM " . tablename('jy_farmer_land') . " WHERE uniacid=:uniacid AND classify_id=:classify_id AND status=1";
		$area = pdo_fetch($sql1,array(":uniacid" => $_SESSION['uniacid'],":classify_id" => $item['id']));
		$item['area'] =$area['SUM(area)'];
		//订单数量
		$lands=pdo_getall('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$item['id']));
		//print_r($lands);exit;
		if($lands){
			$ordernum=0;
			foreach($lands as $rowl){
				$num=pdo_count('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'land_id'=>$rowl['id'],'status in'=>array(1,2)));
				$ordernum=$ordernum+$num;
			}
		$item['ordernum']=$ordernum;
		}
	}
	$return['lclassify']=$lclassify;
	$gclassify=pdo_getall('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid']),'','','paixu asc');
	foreach($gclassify as &$itemg){
		$itemg['types']=pdo_count('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$itemg['id'],'status'=>1));
		//获得最低价格
		$animal=pdo_getall('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$itemg['id'],'status'=>1),'','','price asc limit 1 ');
		$itemg['price']=$animal['0']['price'];
		//订单数量
		if($animal){
			$ordernum=0;
			foreach($animal as $rowa){
				$num=pdo_count('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'guoshu_id'=>$rowa['id'],'status in'=>array(1,2)));
				$ordernum=$ordernum+$num;
			}
			$itemg['ordernum']=$ordernum;
		}
		$itemg['image']=tomedia($itemg['image']);
	}
	
	$return['gclassify']=$gclassify;
	$worker=pdo_getall('jy_farmer_worker',array('uniacid'=>$_SESSION['uniacid']));
	if($worker){
		foreach($worker as &$work){
			$work['headimg']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$work['mid']),'avatar');
		}
	}
	$return['worker']=$worker;
	$aclassify=pdo_getall('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid']),'','','paixu asc');
	foreach($aclassify as &$itema){
		$itema['types']=pdo_count('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$itema['id'],'status'=>1));
		//获得最低价格
		$animal=pdo_getall('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$itema['id'],'status'=>1),'','','price asc limit 1 ');
		//print_r($animal);exit;
		$itema['price']=$animal['0']['price'];
		//订单数量
		if($animal){
			$ordernum=0;
			foreach($animal as $rowac){
				$num=pdo_count('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'animal_id'=>$rowac['id'],'status in'=>array(1,2)));
				$ordernum=$ordernum+$num;
			}
		$itema['ordernum']=$ordernum;
		}
	}
	$return['notices']=pdo_getall('jy_farmer_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),$fields);
	$return['aclassify']=$aclassify;
	$return['setting']=$setting;
	$return['params']=array('jy'=>1);
	$sharelink='jy_newnong/index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$setting['share_title']?$setting['share_title']:'首页-新概念农场',
			'desc'=>$setting['share_desc']?$setting['share_desc']:'新概念农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($setting['share_image'])
	);
	set_html('title',$setting['title']?$setting['title'] :'新概念农场');
	exi($return);
}
